package io.wdb.modules.generator.generator.controller.other;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import org.apache.catalina.WebResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.manager.job.task.CapitalSchedulTask;

@RestController
@RequestMapping("capital/statistics")
public class CapitalController extends BaseController{
	private Logger log = LoggerFactory.getLogger(CapitalController.class);
	@Autowired
	private CapitalSchedulTask capitalSchedulTask;
	
	@RequestMapping("/selectCapital")
	public BaseResponse scheduleCapitalSelect(@RequestParam Map<String,Object> params) {
		log.info("开始统计元数据信息======");
		Date a1 = new Date();
		BaseResponse baseResponse = success();
		WebResponse webResponse = new WebResponse();
		try {
			Date date = new Date();
			Date startTime = null;
			Date endTime = null;
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
			if(params.get("startTime")!=null) {
				startTime = simpleDateFormat.parse(params.get("startTime").toString());
			}else {
				startTime = date;
			}
			if(params.get("endTime")!=null) {
				endTime = simpleDateFormat.parse(params.get("endTime").toString());
			}else {
				endTime = date;
			}
			long diff = endTime.getTime() - startTime.getTime();
			long days = diff / (1000 * 60 * 60 * 24);  
			long hours = diff/(1000* 60 * 60);
			if(endTime.getTime()>startTime.getTime()) {
				for(int i=0;i<hours;i++) {
					Calendar eachTime = Calendar.getInstance(TimeZone.getTimeZone("GMT-8"));
					eachTime.setTime(endTime);
					eachTime.add(Calendar.HOUR_OF_DAY, -i);
					params.put("selectTime", eachTime.getTime());
					capitalSchedulTask.capitalDeposit(params);
				}
			}
			Date a2 = new Date();
			log.info("统计元数据信息花费时间为："+(a2.getTime()-a1.getTime()));
			log.info("统计元数据信息完成======"+params.get("selectTime"));
			webResponse.setCode(100);
			webResponse.setMessage("统计元数据信息成功");
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			log.error("统计元数据信息失败");
			log.error(e.getMessage(),e);
			baseResponse = failed(e);
		}
		return baseResponse;
    }
}
